#include "tracexportsettingswidget.h"
#include "ui_tracexportsettingswidget.h"
#include "tracsettings.h"

#include "xmlrpcbase64.h"
#include "xmlrpcconnection.h"
#include "xmlrpcparser.h"
#include "xmlrpcrequest.h"
#include "xmlrpcresponse.h"

#include <QtCore/QDebug>

using XmlRpc::XmlRpcConnection;
using XmlRpc::XmlRpcRequest;
using XmlRpc::XmlRpcBase64;

namespace Trac {
namespace Internal {

TracExportSettingsWidget::TracExportSettingsWidget(QWidget *parent) :
    QWidget(parent),
    m_valid(false),
    m_connection(new XmlRpcConnection)
{
    ui = new Ui::TracExportSettingsWidget();
    ui->setupUi(this);
    setMinimumWidth(400);

    ui->versionControlComboBox->addItem(Constants::STRING_GIT);
    ui->versionControlComboBox->addItem(Constants::STRING_MERCURIAL);
    ui->versionControlComboBox->addItem(Constants::STRING_SUBVERSION);



    connect(ui->versionControlComboBox, SIGNAL(currentIndexChanged(QString)),
            this, SLOT(checkIsValid()));
    connect(ui->publicRadioButton, SIGNAL(clicked()),
            this, SLOT(checkIsValid()));
    connect(ui->privateRadioButton, SIGNAL(clicked()),
            this, SLOT(checkIsValid()));
    connect(ui->termsOfUseCheckBox, SIGNAL(stateChanged(int)),
            this, SLOT(checkIsValid()));
}

TracExportSettingsWidget::~TracExportSettingsWidget()
{
    delete ui;
}


bool TracExportSettingsWidget::isValid() const
{
    return m_valid;
}

void TracExportSettingsWidget::checkIsValid()
{
    if(validateTermsOfUseCheckBox()) {

        if(ui->versionControlComboBox->currentText() == Constants::STRING_GIT)
            TracSettings::instance()->setBinary(Constants::BINARY_GIT);
        else if(ui->versionControlComboBox->currentText() == Constants::STRING_MERCURIAL)
            TracSettings::instance()->setBinary(Constants::BINARY_MERCURIAL);
        else if(ui->versionControlComboBox->currentText() == Constants::STRING_SUBVERSION)
            TracSettings::instance()->setBinary(Constants::BINARY_SUBVERSION);

        TracSettings::instance()->setVersionControlSystem(ui->versionControlComboBox->currentText());
        TracSettings::instance()->setIsPublicProject(ui->publicRadioButton->isChecked());

        if(m_valid == false) {
            m_valid = true;
            emit validChanged(m_valid);
        }
    }
    else {
        if(m_valid == true) {
            m_valid = false;
            emit validChanged(m_valid);
        }
    }
}

bool TracExportSettingsWidget::validateTermsOfUseCheckBox()
{
    if(ui->termsOfUseCheckBox->checkState() == Qt::Checked)
        return true;
    else
        return false;
}

} // namespace Internal
} // namespace Trac
